Shader "Hidden/DrawTexture"
{
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _SourceTex;
            sampler2D _DrawTex;
            float4 _SizeAndOffset;
            float4 _TilingAndOffset;
            float4 _Color;
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }

            float4 frag (v2f i) : SV_Target
            {
                float4 col = tex2D(_SourceTex, i.uv);

                float2 pos = _SizeAndOffset.zw;
                float2 scale = _SizeAndOffset.xy;

                float2 subUV = (i.uv -pos)/scale*_TilingAndOffset.xy +_TilingAndOffset.zw;

                float clampMinX = _TilingAndOffset.z;
                float clampMinY = _TilingAndOffset.w;
            
                float clampMaxX = _TilingAndOffset.x + _TilingAndOffset.z;
                float clampMaxY =_TilingAndOffset.y + _TilingAndOffset.w;

                if(subUV.x>=clampMinX && subUV.x<clampMaxX     
                && subUV.y>=clampMinY && subUV.y<clampMaxY)
                {
                    col += tex2D(_DrawTex,subUV) *_Color;
                }
                return col;
            }
            ENDCG
        }
    }
}
